﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace FlashPay.Admin.Controllers
{
    using FlashPay.Entity;
    using FlashPay.Entity.Enum;
    using FlashPay.Entity.Parameter;
    using FlashPay.Entity.Request.PaymentInterface;
    using FlashPay.Entity.Response.User;
    using FlashPay.Service.Interface;
    using Microsoft.AspNetCore.Http;

    /// <summary>
    ///第三方接口
    /// </summary>
    public class PaymentInterfaceController : BaseController
    {
        #region 注入
        /// <summary>
        /// 第三方接口
        /// </summary>
        private readonly PaymentInterfaceService _paymentInterfaceService;

        /// <summary>
        /// 日志业务接口
        /// </summary>
        private readonly LogService _logService;

        /// <summary>
        /// 注入
        /// </summary>
        /// <param name="sysRoleService"></param>
        public PaymentInterfaceController(IAuthenticate<TicketResponse> _manage, IHttpContextAccessor _accessor, PaymentInterfaceService paymentInterfaceService, LogService logService) : base(_manage, _accessor)
        {
            _paymentInterfaceService = paymentInterfaceService;
            _logService = logService;
        }
        #endregion


        /// <summary>
        /// 获取第三方接口
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.PaymentInterface0002)]
        public JsonResult GetPaymentInterface(int id)
        {
            //输出
            var response = new JResult<PaymentInterfaceReponse>()
            {
                Success = false
            };

            try
            {
                response.Success = true;
                response.Data = _paymentInterfaceService.GetByPaymentInterface(id, _manage.data.CompanyID, _manage.data.UserPermission);
            }
            catch (Exception ex) {
                response.ErrorMessage = ex.Message;
            }
           
            return Json(response);
        }

        /// <summary>
        /// 付款接口新增或编辑
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.PaymentInterface0002)]
        public JsonResult PaymentInterfaceAddOrEdit(PaymentInterfaceAddOrEditRequest request)
        {
            request.UserName = _manage.data.UserName;
            request.UserPermission = _manage.data.UserPermission;
            var response = _paymentInterfaceService.PaymentInterfaceAddOrUpdate(request);

            return Json(response);
        }

        /// <summary>
        /// 付款记录（日志）
        /// </summary>
        /// <returns>JResult</returns>
        [AuthorizeFilter(AuthCode.PaymentInterface0006)]
        public JsonResult PaymentInterfaceLog(int id)
        {
            var query = new GetListByOrderNoQuery()
            {
                OrderNo = id,
                LogTypes = new List<int>() {
                    (int)LogRecordLogType.SysInterface_Plus,
                    (int)LogRecordLogType.SysInterface_Plus_Auto,
                    (int)LogRecordLogType.SysInterface_Less,
                    (int)LogRecordLogType.SysInterface_Less_Auto,
                }
            };

            var response = _logService.GetListByOrderNo(query);

            return Json(response);
        }

        ///// <summary>
        ///// 修改状态
        ///// </summary>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //[AuthorizeFilter(AuthCode.PaymentInterface0004)]
        //public JsonResult UpdateStatus(PaymentInterfaceQuery query)
        //{
        //    var data = _paymentInterfaceService.UpdateStatus(query);
        //    return Json(data);
        //}

        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //[AuthorizeFilter(AuthCode.PaymentInterface0003)]
        //public JsonResult DeleteInterface(PaymentInterfaceQuery query)
        //{
        //    var data = _paymentInterfaceService.DeleteInterface(query);
        //    return Json(data);
        //}

        /// <summary>
        /// 增加、减少余额
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.PaymentInterface0006)]
        public JsonResult PaymentInterfaceBalance(PaymentInterfaceBalanceRequest request)
        {
            request.UserName = _manage.data.UserName;
            

            var response = _paymentInterfaceService.PaymentInterfaceBalance(request);

            return Json(response);
        }


    }
}